package com.muyu.danmo.service.impl;

import com.muyu.common.core.utils.DateUtils;
import com.muyu.common.core.utils.StringUtils;
import com.muyu.danmo.domain.DanmoAssociation;
import com.muyu.danmo.domain.DanmoAssociationType;
import com.muyu.danmo.mapper.DanmoAssociationMapper;
import com.muyu.danmo.service.IDanmoAssociationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 协会Service业务层处理
 *
 * @author yangle
 * @date 2024-09-27
 */
@Service
public class DanmoAssociationServiceImpl implements IDanmoAssociationService
{
    @Autowired
    private DanmoAssociationMapper danmoAssociationMapper;

    /**
     * 查询协会
     *
     * @param id 协会主键
     * @return 协会
     */
    @Override
    public DanmoAssociation selectDanmoAssociationById(Long id)
    {
        return danmoAssociationMapper.selectDanmoAssociationById(id);
    }

    /**
     * 查询协会列表
     *
     * @param danmoAssociation 协会
     * @return 协会
     */
    @Override
    public List<DanmoAssociation> selectDanmoAssociationList(DanmoAssociation danmoAssociation)
    {
        return danmoAssociationMapper.selectDanmoAssociationList(danmoAssociation);
    }

    /**
     * 新增协会
     *
     * @param danmoAssociation 协会
     * @return 结果
     */
    @Transactional
    @Override
    public int insertDanmoAssociation(DanmoAssociation danmoAssociation)
    {
        danmoAssociation.setCreateTime(DateUtils.getNowDate());
        int rows = danmoAssociationMapper.insertDanmoAssociation(danmoAssociation);
        insertDanmoAssociationType(danmoAssociation);
        return rows;
    }

    /**
     * 修改协会
     *
     * @param danmoAssociation 协会
     * @return 结果
     */
    @Transactional
    @Override
    public int updateDanmoAssociation(DanmoAssociation danmoAssociation)
    {
        danmoAssociation.setUpdateTime(DateUtils.getNowDate());
        danmoAssociationMapper.deleteDanmoAssociationTypeByAssociationId(danmoAssociation.getId());
        insertDanmoAssociationType(danmoAssociation);
        return danmoAssociationMapper.updateDanmoAssociation(danmoAssociation);
    }

    /**
     * 批量删除协会
     *
     * @param ids 需要删除的协会主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDanmoAssociationByIds(Long[] ids)
    {
        danmoAssociationMapper.deleteDanmoAssociationTypeByAssociationIds(ids);
        return danmoAssociationMapper.deleteDanmoAssociationByIds(ids);
    }

    /**
     * 删除协会信息
     *
     * @param id 协会主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDanmoAssociationById(Long id)
    {
        danmoAssociationMapper.deleteDanmoAssociationTypeByAssociationId(id);
        return danmoAssociationMapper.deleteDanmoAssociationById(id);
    }

    /**
     * 新增艺术家类型名称信息
     *
     * @param danmoAssociation 协会对象
     */
    public void insertDanmoAssociationType(DanmoAssociation danmoAssociation)
    {
        List<DanmoAssociationType> danmoAssociationTypeList = danmoAssociation.getDanmoAssociationTypeList();
        Long id = danmoAssociation.getId();
        if (StringUtils.isNotNull(danmoAssociationTypeList))
        {
            List<DanmoAssociationType> list = new ArrayList<DanmoAssociationType>();
            for (DanmoAssociationType danmoAssociationType : danmoAssociationTypeList)
            {
                danmoAssociationType.setAssociationId(id);
                list.add(danmoAssociationType);
            }
            if (list.size() > 0)
            {
                danmoAssociationMapper.batchDanmoAssociationType(list);
            }
        }
    }
}
